package com.ulook.lookplanet.controller;

import com.ulook.lookplanet.bean.TSysMsg;
import com.ulook.lookplanet.bean.TUserMsg;
import com.ulook.lookplanet.bean.VMsgUser;
import com.ulook.lookplanet.common.ExceptionCode;
import com.ulook.lookplanet.common.JsonResult;
import com.ulook.lookplanet.service.NotificationService;
import com.ulook.lookplanet.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 描述：
 *
 * @author L1998
 * @date 2021/4/29
 */
@RestController
@RequestMapping("/notif")
public class NotificationController {

    @Autowired
    private NotificationService service;

    /**
     * 查询系统消息
     */
    @PostMapping("/querySysMsg")
    public JsonResult<List<TSysMsg>> querySysMsg(HttpServletRequest request){
        String userid = request.getParameter("userid");
        if(StringUtil.isEmpty(userid)){
            return new JsonResult<>(ExceptionCode.NULL_PARAM, "参数为空");
        }
        List<TSysMsg> sysMsgList = service.querySysMsg(Integer.parseInt(userid));
        //System.out.println(sysMsgList.toString());
        if(sysMsgList == null){
            return new JsonResult<>(ExceptionCode.UNKNOWN_ERROR, "查询失败");
        }
        return new JsonResult<>(sysMsgList);
    }

    /**
     * 查询用户消息
     */
    @PostMapping("/queryUserMsg")
    public JsonResult<List<VMsgUser>> queryUserMsg(HttpServletRequest request){
        String touser = request.getParameter("userid");
        if(StringUtil.isEmpty(touser)){
            return new JsonResult<>(ExceptionCode.NULL_PARAM, "参数为空");
        }
        List<VMsgUser> userMsgList = service.queryUserMsg(Integer.parseInt(touser));
        //System.out.println(userMsgList.toString());
        if(userMsgList == null){
            return new JsonResult<>(ExceptionCode.UNKNOWN_ERROR, "查询失败");
        }
        return new JsonResult<>(userMsgList);
    }

    /**
     * 发送消息
     */
    @PostMapping("/sendUserMsg")
    public JsonResult sendUserMsg(HttpServletRequest request){
        //#{uid}, #{fromuser}, #{touser}, #{sendtime}, 0, #{msg}
        String uid = request.getParameter("uid");
        String fromuser = request.getParameter("fromuser");
        String touser = request.getParameter("touser");
        String sendtime = request.getParameter("sendtime");
        String msg = request.getParameter("msg");

        if(StringUtil.isEmpty(uid)){
            return new JsonResult<>(ExceptionCode.NULL_PARAM, "参数为空");
        }

        TUserMsg userMsg = new TUserMsg();
        userMsg.setUid(uid);
        userMsg.setFromuser(Integer.parseInt(fromuser));
        userMsg.setTouser(Integer.parseInt(touser));
        userMsg.setSendtime(sendtime);
        userMsg.setMsg(msg);

        service.addUserMsg(userMsg);

        return new JsonResult();
    }

}
